package com.example.baseutils.usb;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;


import com.example.baseutils.application.BaseApplication;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Author by lp,on 2021/12/22/022,14:07.
 */
public class UsbChangeReceiver extends BroadcastReceiver {
    private static final String TAG = "UsbDevBroadcastReceiver";
    private List<UsbStateListtenr> listtenrList;
    protected List<String> currentBusList = new ArrayList<>();
    protected List<UsbDevice> devices = new ArrayList<>();

    public UsbChangeReceiver(List<UsbStateListtenr> listtenrList) {
        this.listtenrList = listtenrList;
        findUsbList();
        notify(currentBusList, devices);
    }

    public void findUsbList() {
//        currentBusList.addAll(findUsbBusList());
        currentBusList.clear();
        devices.clear();
        List<UsbDevice> usbDevices = UsbDeviceUtil.scan(BaseApplication.getInstance());
        devices.addAll(usbDevices);
        for (UsbDevice device : devices) {
            UsbBusInfo usbBusInfo = UsbDeviceBusFinder.parseUsbDevice(device);
            currentBusList.add(usbBusInfo.getBus());
        }
        Log.i(TAG, "UsbChangeReceiver: " + Arrays.toString(currentBusList.toArray()));
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        Log.e(TAG, "onReceive: action-->" + action);
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            Toast.makeText(context, "USB设备插入", Toast.LENGTH_LONG).show();
            final UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            if (device != null) {
                Log.e(TAG, "onReceive: =============ACTION_USB_DEVICE_ATTACHED==========");
                UsbBusInfo usbBusInfo = UsbDeviceBusFinder.parseUsbDevice(device);
                if (usbBusInfo != null && !TextUtils.isEmpty(usbBusInfo.getBus())) {
                    if (!currentBusList.contains(usbBusInfo.getBus())) {
                        Log.i(TAG, "插入一个设备: ");
                        currentBusList.add(usbBusInfo.getBus());
                    }
                    notify(usbBusInfo.getBus(), device, true);
                }
            }

        } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            Toast.makeText(context, "USB设备拔出", Toast.LENGTH_LONG).show();
            final UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            if (device != null) {
                Log.e(TAG, "onReceive: =============ACTION_USB_DEVICE_DETACHED==========");
                String outBus = findOutBus();
                notify(outBus, device, false);
            }
        }
    }


    protected void notify(String bus, UsbDevice device, boolean state) {
        for (UsbStateListtenr usbStateListtenr : listtenrList) {
            usbStateListtenr.usbChange(bus, device, state);
        }

    }

    protected void notify(List<String> busLsit, List<UsbDevice> devices) {
        for (UsbStateListtenr usbStateListtenr : listtenrList) {
            usbStateListtenr.usbInitList(busLsit, devices);
        }

    }

    private String findOutBus() {
        List<UsbBusInfo> list = UsbDeviceBusFinder.list();
        List<String> outList = new ArrayList<>();
        List<String> newList = new ArrayList<>();
        for (UsbBusInfo usbBusInfo : list) {
            outList.add(usbBusInfo.getBus());
            newList.add(usbBusInfo.getBus());
        }
        //求一个差集，差集为currentBusList
        currentBusList.removeAll(outList);
        outList.clear();
        outList.addAll(currentBusList);
        currentBusList.clear();
        currentBusList.addAll(newList);
        Log.i(TAG, "new Usb list: " + Arrays.toString(outList.toArray()));
        //求差集，即拔出的设备
        if (outList.size() > 0) {
            return outList.get(0);
        }
        return null;
    }

    private List<String> findUsbBusList() {
        List<UsbBusInfo> list = UsbDeviceBusFinder.list();
        List<String> newList = new ArrayList<>();
        for (UsbBusInfo usbBusInfo : list) {
            newList.add(usbBusInfo.getBus());
        }
        return newList;
    }

}
